import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import './touch_callback.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'package:fluttertoast/fluttertoast.dart';

class WechatItem extends StatelessWidget {
  Key? key;
  final String? title;
  final String? imagePath;
  final Icon? icon;
  final String? trailingText;
  final VoidCallback? onTap;

  WechatItem({this.key, this.title, this.imagePath, this.icon, this.trailingText, this.onTap}) : super(key: key);

  Future<void> _saveData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString('name', '镇景雄');
    await prefs.setString('phone', '13308622037');
    print('Data saved to local storage');

    // 获取Documents目录路径
    final directory = await getApplicationDocumentsDirectory();
    print("Documents Directory: ${directory.path}");
  }

  @override
  Widget build(BuildContext context) {
    return TouchCallBack(
      onPressed: () async {
        if (onTap != null) {
          onTap!();
        } else {
          switch (title) {
            case '朋友圈':
              Navigator.pushNamed(context, '/friends');
              break;
            case '收藏':
              break;
            case '设置':
              await _saveData();
              break;
          }
        }
      },
      child: Container(
        height: 50.0,
        child: Row(
          children: <Widget>[
            Container(
              child: imagePath != null
                  ? Image.asset(
                imagePath!,
                width: 32.0,
                height: 32.0,
              )
                  : SizedBox(
                height: 32.0,
                width: 32.0,
                child: icon,
              ),
              margin: const EdgeInsets.only(left: 22.0, right: 20.0),
            ),
            Expanded(
              child: Text(
                title!,
                style: TextStyle(fontSize: 16.0, color: Color(0xFF353535)),
              ),
            ),
            if (trailingText != null)
              Container(
                margin: const EdgeInsets.only(right: 22.0),
                child: Text(
                  trailingText!,
                  style: TextStyle(fontSize: 14.0, color: Color(0xFFa9a9a9)),
                ),
              ),
          ],
        ),
      ),
    );
  }
}